PICKAXE_ID = 4
BOMB_ID = 5
WARPSTAFF_ID = 6
TERRAIN_TAG_WALL = 1
LAYER_WALL = 1

class Scene_Map
  # 重写main方法
  def main
    # 生成活动块
    @spriteset = Spriteset_Map.new
    # 生成信息窗口
    @message_window = Window_Message.new
    # 生成角色状态窗口
    @status_window = Window_ActorStatus.new
    @status_window.z = 3100
    # 生成怪物伤害显示
    @damage_window = Window_MapDamage.new
    @damage_window.z = 3100
    # 执行过渡
    Graphics.transition
    # 主循环
    loop do
      # 刷新游戏画面
      Graphics.update
      # 刷新输入信息
      Input.update
      # 刷新画面
      update
      # 如果画面切换的话就中断循环
      if $scene != self
        break
      end
    end
    # 准备过渡
    Graphics.freeze
    # 释放活动块
    @spriteset.dispose
    # 释放窗口
    @message_window.dispose
    @status_window.dispose
    @damage_window.dispose
    # 标题画面切换中的情况下
    if $scene.is_a?(Scene_Title)
      # 淡入淡出画面
      Graphics.transition
      Graphics.freeze
    end
  end

  # 重写update方法
  def update
    # 循环
    loop do
      # 按照地图、实例、主角的顺序刷新
      # (本更新顺序不会在的满足事件的执行条件下成为给予角色瞬间移动
      #  的机会的重要因素)
      $game_map.update
      $game_system.map_interpreter.update
      $game_player.update
      # 系统 (计时器)、画面刷新
      $game_system.update
      $game_screen.update
      # 如果主角在场所移动中就中断循环
      unless $game_temp.player_transferring
        break
      end
      # 执行场所移动
      transfer_player
      # 处理过渡中的情况下、中断循环
      if $game_temp.transition_processing
        break
      end
    end
    # 刷新活动块
    @spriteset.update
    # 刷新窗口
    @message_window.update
    @status_window.refresh
    @status_window.update
    @damage_window.refresh
    @damage_window.update
    # 游戏结束的情况下
    if $game_temp.gameover
      # 切换的游戏结束画面
      $scene = Scene_Gameover.new
      return
    end
    # 返回标题画面的情况下
    if $game_temp.to_title
      # 切换到标题画面
      $scene = Scene_Title.new
      return
    end
    # 处理过渡中的情况下
    if $game_temp.transition_processing
      # 清除过渡处理中标志
      $game_temp.transition_processing = false
      # 执行过渡
      if $game_temp.transition_name == ""
        Graphics.transition(20)
      else
        Graphics.transition(40, "Graphics/Transitions/" +
          $game_temp.transition_name)
      end
    end
    # 显示信息窗口中的情况下
    if $game_temp.message_window_showing
      return
    end
    # 遇敌计数为 0 且、且遇敌列表不为空的情况下
    if $game_player.encounter_count == 0 and $game_map.encounter_list != []
      # 不是在事件执行中或者禁止遇敌中
      unless $game_system.map_interpreter.running? or
             $game_system.encounter_disabled
        # 确定队伍
        n = rand($game_map.encounter_list.size)
        troop_id = $game_map.encounter_list[n]
        # 队伍有效的话
        if $data_troops[troop_id] != nil
          # 设置调用战斗标志
          $game_temp.battle_calling = true
          $game_temp.battle_troop_id = troop_id
          $game_temp.battle_can_escape = true
          $game_temp.battle_can_lose = false
          $game_temp.battle_proc = nil
        end
      end
    end
    # 按下 ESC 键的情况下
    if Input.trigger?(Input::B)
      # 不是在事件执行中或菜单禁止中
      unless $game_system.map_interpreter.running? or
             $game_system.menu_disabled
        # 设置菜单调用标志以及 SE 演奏
        $game_temp.menu_calling = true
        $game_temp.menu_beep = true
      end
    end
    # 按下 H 键的情况下
    if Kboard.trigger?($R_Key_H)
      # 不是在事件执行中则设置调用标志
      @enemyinfo_calling = true unless $game_system.map_interpreter.running?
    end
    # 按下 P 键的情况下
    if Kboard.trigger?($R_Key_P)
      # 使用破墙镐
      use_pickaxe unless $game_system.map_interpreter.running?
    end
    # 按下 B 键的情况下
    if Kboard.trigger?($R_Key_B)
      # 使用炸弹
      use_bomb unless $game_system.map_interpreter.running?
    end
    # 按下 W 键的情况下
    if Kboard.trigger?($R_Key_W)
      # 使用中心对称飞行器
      use_warpstaff unless $game_system.map_interpreter.running?
    end
    # 调试模式为 ON 并且按下 F9 键的情况下
    if $DEBUG and Input.press?(Input::F9)
      # 设置调用调试标志
      $game_temp.debug_calling = true
    end
    # 不在主角移动中的情况下
    unless $game_player.moving?
      # 执行各种画面的调用
      if $game_temp.battle_calling
        call_battle
      elsif $game_temp.shop_calling
        call_shop
      elsif $game_temp.name_calling
        call_name
      elsif $game_temp.menu_calling
        call_menu
      elsif $game_temp.save_calling
        call_save
      elsif $game_temp.debug_calling
        call_debug
      elsif @enemyinfo_calling
        call_enemyinfo
      end
    end
  end
  
  # 调用怪物手册
  def call_enemyinfo
    # 清除调用标志
    @enemyinfo_calling = false
    # 演奏SE
    $game_system.se_play($data_system.decision_se)
    # 矫正主角姿势
    $game_player.straighten
    # 切换到菜单画面
    $scene = Scene_EnemyInfo.new
  end

  # 使用破墙镐
  def use_pickaxe
    # 如果物品用完的情况下
    if $game_party.item_number(PICKAXE_ID) == 0
      # 演奏冻结SE
      $game_system.se_play($data_system.buzzer_se)
      return 0
    end
    # 确定角色面向位置
    x = $game_player.x
    y = $game_player.y
    case $game_player.direction
    when 2
      y += 1
    when 4
      x -= 1
    when 6
      x += 1
    when 8
      y -= 1
    end
    # 获取地形标志
    t_tag = $game_map.terrain_tag(x, y)
    if t_tag == TERRAIN_TAG_WALL then
      # 清除地图第二层
      $game_map.data[x, y, LAYER_WALL] = 0
      # 消耗物品
      if $data_items[PICKAXE_ID].consumable
        $game_party.lose_item(PICKAXE_ID, 1)
      end
      return 1
    end
    return -1
  end

  # 使用炸弹
  def use_bomb
    # 如果炸弹用完的情况下
    if $game_party.item_number(BOMB_ID) == 0
      # 演奏冻结SE
      $game_system.se_play($data_system.buzzer_se)
      return 0
    end
    # 确定角色面向位置
    x = $game_player.x
    y = $game_player.y
    case $game_player.direction
    when 2
      y += 1
    when 4
      x -= 1
    when 6
      x += 1
    when 8
      y -= 1
    end
    # 获取怪物信息
    for event in $game_map.events.values
      # 如果坐标不正确或者不是怪物或者怪物死亡则继续
      next unless event.x == x and event.y == y
      next unless event.event.name[0,2] == '_M'
      key = [$game_map.map_id, event.id, 'A']
      next if $game_self_switches[key] == true
      # 打开怪物的独立开关A
      $game_self_switches[key] = true
      $game_map.need_refresh = true
      # 消耗物品
      if $data_items[BOMB_ID].consumable
        $game_party.lose_item(BOMB_ID, 1)
      end
      return 1
    end
    return -1
  end

  # 使用中心对称飞行器
  def use_warpstaff
    # 如果飞行器用完的情况下
    if $game_party.item_number(WARPSTAFF_ID) == 0
      # 演奏冻结SE
      $game_system.se_play($data_system.buzzer_se)
      return 0
    end
    # 确定角色将会飞到的位置
    x = 24 - $game_player.x
    y = 14 - $game_player.y
    # 如果目标格无法到达则不能飞行
    return -1 unless $game_map.passable?(x,y,0)
    # 获取怪物信息
     for event in $game_map.events.values
      # 如果目标地点有东西就不能飞行
      next unless event.x == x and event.y == y
      key = [$game_map.map_id, event.id, 'A']
      next if $game_self_switches[key] == true
      return -1
    end
    # 设置场所移动标志
    $game_temp.player_transferring = true
    # 设置新的位置
    $game_temp.player_new_map_id = $game_map.map_id
    $game_temp.player_new_x = x
    $game_temp.player_new_y = y
    $game_temp.player_new_direction = $game_player.direction
    # 消耗物品
    if $data_items[WARPSTAFF_ID].consumable
      $game_party.lose_item(WARPSTAFF_ID, 1)
    end
    return 1
  end
end
